#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
struct car{
    string id;
    int moment;
    string status;
};

bool cmp1(car a,car b){
    if(a.id!=b.id)return (a.id<b.id);
    if(a.moment!=b.moment) return (a.moment<b.moment);
}

bool cmp2(car a,car b){
    if(a.moment!=b.moment){return a.moment<b.moment;}
    if(a.id!=b.id){return a.id<b.id;}
}

bool cmp3(map<string,int>::iterator a,map<string,int>::iterator b){
    return a->second>b->second;
}
vector<car> record;
map<string,int> lastime;
int main(){
    //freopen("in.txt","r",stdin);
    int N,K;
    scanf("%d %d",&N,&K);
    record.resize(N);
    string carid,stat;
    int hour,minu,sec;
    for(int i=0;i<N;i++){
        cin>>carid;
        scanf("%d:%d:%d",&hour,&minu,&sec);
        cin>>stat;
        record[i].id=carid;
        record[i].status=stat;
        record[i].moment=3600*hour+60*minu+sec;
    }
    sort(record.begin(),record.end(),cmp1);
    vector<car> realrcd;
    int maxtime=0;
    for(int i=0;i<N-1;i++){
        if(record[i].status=="in" && record[i+1].status=="out" && record[i].id==record[i+1].id){
            realrcd.push_back(record[i]);
            realrcd.push_back(record[i+1]);
            if(lastime.find(record[i].id)==lastime.end()){
                lastime[record[i].id]=record[i+1].moment-record[i].moment;
                if(lastime[record[i].id]>maxtime){maxtime=lastime[record[i].id];}
            }
            else{
                lastime[record[i].id]+=record[i+1].moment-record[i].moment;
                if(lastime[record[i].id]>maxtime){maxtime=lastime[record[i].id];}
            }
        }
    }
    sort(realrcd.begin(),realrcd.end(),cmp2);
    vector<int> timelist(86400);
    int cnt=0;
    fill(timelist.begin(),timelist.begin()+realrcd[0].moment,cnt++);
    for(int i=1;i<realrcd.size();i++){
        if(realrcd[i].status=="in"){
            fill(timelist.begin()+realrcd[i-1].moment,timelist.begin()+realrcd[i].moment,cnt++);
        }
        else if(realrcd[i].status=="out"){
            fill(timelist.begin()+realrcd[i-1].moment,timelist.begin()+realrcd[i].moment,cnt--);
        }
    }
    fill(timelist.begin()+realrcd.back().moment,timelist.end(),cnt);
    for(int i=0;i<K;i++){
        scanf("%d:%d:%d",&hour,&minu,&sec);
        int tmp=hour*3600+minu*60+sec;
        printf("%d\n",timelist[tmp]);
    }
    map<string,int>::iterator it=lastime.begin();
    for(;it!=lastime.end();it++){
        if(it->second==maxtime){
            cout<<it->first<<" ";
        }
    }
    hour=maxtime/3600;
    minu=(maxtime-hour*3600)/60;
    sec=(maxtime-hour*3600-minu*60);
    printf("%02d:%02d:%02d\n",hour,minu,sec);
}
